package com.albaba.designpattern.builder.text_002;

/**
 * @author zx
 * @version 1.0
 * @description TODO
 * @date 2025/2/19 20:47
 */
public class RabbitMQClient_002 {
    private String host = "localhost";

    private int port = 5672;

    private int mode;

    private String exchange;

    private String queue;

    private boolean isDurable = true;

    int connectionTimeout = 1000;

    private RabbitMQClient_002(){

    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getMode() {
        return mode;
    }

    public void setMode(int mode) {
        if (mode == 1) {
            if (exchange != null) {
                throw new RuntimeException("工作队列模式无需设计交换机");
            }
            if (queue == null || queue.equals("")) {
                throw new RuntimeException("工作队列模式名称不能为空");
            }
            if (!isDurable){
                throw new RuntimeException("工作队列模式必须开启持久化");
            }
        }else if (mode == 2) {
            if (exchange == null || exchange.equals("")) {
                throw new RuntimeException("路由模式下必须设置交换机");
            }
            if (queue != null) {
                throw new RuntimeException("路由模式下不需要设置队列名称");
            }
        }
        this.mode = mode;
    }

    public String getExchange() {
        return exchange;
    }

    public void setExchange(String exchange) {
        if (mode == 1) {
            if (exchange != null) {
                throw new RuntimeException("工作队列模式无需设计交换机");
            }
            if (queue == null || queue.equals("")) {
                throw new RuntimeException("工作队列模式名称不能为空");
            }
            if (!isDurable){
                throw new RuntimeException("工作队列模式必须开启持久化");
            }
        }else if (mode == 2) {
            if (exchange == null || exchange.equals("")) {
                throw new RuntimeException("路由模式下必须设置交换机");
            }
            if (queue != null) {
                throw new RuntimeException("路由模式下不需要设置队列名称");
            }
        }
        this.exchange = exchange;
    }

    public String getQueue() {
        return queue;
    }

    public void setQueue(String queue) {
        this.queue = queue;
    }

    public boolean isDurable() {
        return isDurable;
    }

    public void setDurable(boolean durable) {
        isDurable = durable;
    }

    public int getConnectionTimeout() {
        return connectionTimeout;
    }

    public void setConnectionTimeout(int connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
    }

    public static void main(String[] args) {
        RabbitMQClient_002 client = new RabbitMQClient_002();
        client.setHost("localhost");
        client.setPort(5672);
        client.setMode(1);
        client.setQueue("hello");
        client.setExchange(null);
        client.setConnectionTimeout(1000);
        client.setDurable(true);
        client.setDurable(true);

    }
}
